package com.ffsky.template.demo.data;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.ffsky.template.demo.R;
import com.ffsky.template.demo.span.LinkSpan;
import com.gitee.hljdrl.dialog.UIPOPDialog;
import com.gitee.hljdrl.dialog.basis.OnEventListener;
import com.gitee.hljdrl.dialog.basis.OnVisitedListener;
import com.gitee.hljdrl.dialog.entity.SheetEntity;
import com.gitee.hljdrl.dialog.util.UIUtil;
import com.gitee.hljdrl.dialog.visitor.DownloadVisitor;
import com.gitee.hljdrl.dialog.visitor.ProgressVisitor;
import com.gitee.hljdrl.dialog.visitor.PromptEditVisitor;
import com.gitee.hljdrl.dialog.visitor.PromptVisitor;
import com.gitee.hljdrl.dialog.visitor.SheetVisitor;
import com.gitee.hljdrl.ffkit.app.FFKitAboutActivity;
import com.gitee.hljdrl.ffkit.bean.FFKitLink;
import com.gitee.hljdrl.ffkit.builder.FFKitLinkSource;
import com.gitee.hljdrl.ffkit.fragment.FFKitColorFragment;
import com.gitee.hljdrl.ffkit.listener.OnExecuteListener;
import com.gitee.hljdrl.ffkit.util.FFKitUtil;
import com.gitee.hljdrl.utils.string.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class MyLinkSource extends FFKitLinkSource {


    @Override
    public List<FFKitLink> getList() {
        List<FFKitLink> list = new ArrayList<>();
        //----------------------------------------------------------------

        list.add(new FFKitLink.Builder().setName("Hello World!").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {
                Toast.makeText(activity, "Hello World!", Toast.LENGTH_SHORT).show();
            }
        }).build());

        list.add(new FFKitLink.Builder().setName("Hello World! Activity").setActivityClass(FFKitAboutActivity.class).build());
        //----------------------------------------------------------------
        list.add(new FFKitLink.Builder().setName("Hello World! Fragment").setFragmentClass(FFKitColorFragment.class).build());
        //----------------------------------------------------------------
        list.add(new FFKitLink.Builder().setName("Hello World! Fragment List").addFragmentClass(FFKitColorFragment.class, FFKitColorFragment.class, FFKitColorFragment.class, FFKitColorFragment.class).build());
        //----------------------------------------------------------------
        list.add(new FFKitLink.Builder().setName("UIDialog").setDesc("PromptVisitor").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {
                PromptVisitor dialog = new PromptVisitor(activity);
                dialog.setTitle("提示");
                dialog.setContentGravity(Gravity.LEFT);
                dialog.setContent("\u3000\u3000Android 16 开发者预览版 1 现已推出! Android 16 的首个开发者预览版现已发布,您可以在其中测试自己的应用。今后,我们会更频繁地发布 Android 版本,计划在 2025 年发布两个 SDK,这是我们为加快应用和设备创新步伐而采取的诸多措施之一。");
                dialog.setLeftText("确定");
                dialog.setOnEventListener(new OnEventListener() {
                    @Override
                    public void onItemClick(boolean b, Dialog dialog) {
                        dialog.dismiss();

                    }
                });
                dialog.show();
            }
        }).build());

        list.add(new FFKitLink.Builder().setName("UIDialog").setDesc("PromptVisitor长文本").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {
                PromptVisitor dialog = new PromptVisitor(activity);
                dialog.setTitle("提示");
                dialog.setContentGravity(Gravity.LEFT);
                dialog.setContent(activity.getString(R.string.max_text));
                dialog.setLeftText("确定");
                dialog.setRightText("取消");
                dialog.setOnEventListener(new OnEventListener() {
                    @Override
                    public void onItemClick(boolean b, Dialog dialog) {
                        dialog.dismiss();

                    }
                });
                dialog.show();
            }
        }).build());

        list.add(new FFKitLink.Builder().setName("UIDialog").setDesc("PromptVisitor EditText").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {
                PromptEditVisitor promptEditVisitor = new PromptEditVisitor(activity);
                promptEditVisitor.setTitle("提示");
                promptEditVisitor.setGravity(Gravity.TOP);
                promptEditVisitor.setContentGravity(Gravity.LEFT);
                promptEditVisitor.setLeftText("确定");
                promptEditVisitor.setRightText("取消");
                promptEditVisitor.setOnEventListener(new OnEventListener() {
                    @Override
                    public void onItemClick(boolean b, Dialog dialog) {
                        dialog.dismiss();
                        Toast.makeText(activity, promptEditVisitor.getInputText(), Toast.LENGTH_SHORT).show();

                    }
                });
                promptEditVisitor.show();
            }
        }).build());

        list.add(new FFKitLink.Builder().setName("UIDialog").setDesc("PromptVisitor EditText Layout Center").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {
                PromptEditVisitor promptEditVisitor = new PromptEditVisitor(activity, R.style.UIDialogNoAnim, R.layout.dialog_prompt_edit_center_part);
                promptEditVisitor.setTitle("提示");
                promptEditVisitor.setContentGravity(Gravity.LEFT);
                promptEditVisitor.setLeftText("确定");
                promptEditVisitor.setRightText("取消");
                promptEditVisitor.setOnEventListener(new OnEventListener() {
                    @Override
                    public void onItemClick(boolean b, Dialog dialog) {
                        dialog.dismiss();
                        Toast.makeText(activity, promptEditVisitor.getInputText(), Toast.LENGTH_SHORT).show();

                    }
                });
                promptEditVisitor.show();
            }
        }).build());


        list.add(new FFKitLink.Builder().setName("UIDialog").setDesc("ProgressVisitor").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {
                ProgressVisitor progressVisitor = new ProgressVisitor(activity);
                progressVisitor.setTimeDismiss(3000);
                progressVisitor.show();
            }
        }).build());
        list.add(new FFKitLink.Builder().setName("UIDialog").setDesc("DownloadVisitor").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {
                DownloadVisitor progressVisitor = new DownloadVisitor(activity);
                progressVisitor.setTitle("更新组件");
                progressVisitor.setProgressText("下载模块1");
                progressVisitor.setOnVisitedListener(new OnVisitedListener() {
                    @Override
                    public void onVisited(Dialog dialog, View view) {
                        progressVisitor.setProgress(0);
                        progressVisitor.setProgressMax(100);
                        view.postDelayed(new Runnable() {
                            int count = 0;

                            @Override
                            public void run() {
                                count += 1;
                                if (count < 100) {
                                    progressVisitor.setProgress(count);
                                    view.postDelayed(this::run, 100);
                                } else {
                                    UIUtil.dismissDialog(dialog);
                                }
                            }
                        }, 100);
                    }
                });
                progressVisitor.show();

            }
        }).build());

        list.add(new FFKitLink.Builder().setName("UISheetDialog").setDesc("SheetVisitor").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {
                List<SheetEntity> sheetEntities = new ArrayList<>();
                int size = FFKitUtil.getRandomInt(10, 100);
                for (int i = 0; i < size; i++) {
                    sheetEntities.add(new SheetEntity(String.valueOf(i), String.valueOf(i)));
                }
                SheetVisitor sheetVisitor = new SheetVisitor(activity);
                sheetVisitor.setTitle("选择");
                sheetVisitor.setList(sheetEntities);
                sheetVisitor.setItemHeight(UIUtil.dp2px(activity, 50));
                sheetVisitor.setCancelable(false);
                sheetVisitor.setCanceledOnTouchOutside(false);
                Dialog show = sheetVisitor.show();
                //fix 禁止划动消失
                show.setCanceledOnTouchOutside(true);
            }
        }).build());
        //----------------------------------------------------------------
        list.add(new FFKitLink.Builder().setName("PromptVisitor隐私政策提示框").setDesc("PromptVisitor").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {
                PromptVisitor dialog = new PromptVisitor(activity);
                dialog.setTitle("服务协议和隐私政策");
                dialog.setTitleTextAppearance(R.style.agree_title_style);
                String body = activity.getString(R.string.dialog_clause_body);
                SpannableStringBuilder spannableString = new SpannableStringBuilder(body);

                String terms_of_service_link = activity.getString(R.string.terms_of_service_link);
                String privacy_protocol_link = activity.getString(R.string.privacy_protocol_link);
                String authority_link = activity.getString(R.string.authority_link);

                int link_color = Color.parseColor("#27B6F8");
                spannableString.clearSpans();
                /**
                 * 服务协议
                 * */
                int startIndex = body.indexOf(terms_of_service_link);
                int endIndex = startIndex + terms_of_service_link.length();
                spannableString.setSpan(new LinkSpan(activity.getString(R.string.url_service_agreement), link_color),
                        startIndex, endIndex,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                /**
                 * 隐私政策
                 * */
                startIndex = body.indexOf(privacy_protocol_link);
                endIndex = startIndex + privacy_protocol_link.length();
                spannableString.setSpan(new LinkSpan(activity.getString(R.string.url_privacy_clause), link_color),
                        startIndex, endIndex,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                /**
                 * 权限使用规则
                 * */
                startIndex = body.indexOf(authority_link);
                endIndex = startIndex + authority_link.length();
                final String link_authority = StringUtil.string("https://gitee.com", "?time=", System.currentTimeMillis());
                spannableString.setSpan(new LinkSpan(link_authority, link_color),
                        startIndex, endIndex,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                dialog.setContent(spannableString);
                dialog.setCancelable(false);
                dialog.setLeftText(R.string.action_dialog_refuse);
                dialog.setRightText(R.string.action_dialog_agree);
                dialog.setContentGravity(Gravity.LEFT);
                dialog.setOnEventListener(new OnEventListener() {
                    @Override
                    public void onItemClick(boolean isLeft, Dialog dialog) {
                        dialog.dismiss();
                        if (isLeft) {
                            activity.finish();
                        } else {
                            //TODO
//                            agreePrivacyAgreement();
                        }
                    }
                });
                dialog.show();
            }
        }).build());
        //----------------------------------------------------------------
        list.add(new FFKitLink.Builder().setName("PopupWindow").setDesc("PopupWindow").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {
                // 假设你已经有了一个布局文件popup_layout.xml，这是PopupWindow的内容
                LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View popupView = inflater.inflate(R.layout.dialog_prompt_edit_top_part, null);
                // 创建PopupWindow实例
                final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

                // 设置PopupWindow可以获取焦点，否则无法响应点击事件
                popupWindow.setFocusable(true);

                // 设置PopupWindow的外部点击事件，点击外部时PopupWindow消失
                popupWindow.setOutsideTouchable(true);
                popupWindow.setAnimationStyle(R.style.UIDialogTopInAnim);

                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        setWindowBackgroundDim(activity, false);
                    }
                });

                View left_view = popupView.findViewById(R.id.dialog_btn_left);
                View right_view = popupView.findViewById(R.id.dialog_btn_right);
                if (left_view != null) {
                    left_view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            popupWindow.dismiss();
                        }
                    });
                }

                if (right_view != null) {
                    right_view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            popupWindow.dismiss();
                        }
                    });
                }

                // 显示PopupWindow，设置PopupWindow顶部对齐
                popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                setWindowBackgroundDim(activity, true);
            }
        }).build());
        //----------------------------------------------------------------
        list.add(new FFKitLink.Builder().setName("UIPOPDialog").setDesc("UIPOPDialog").setOnExecuteListener(new OnExecuteListener() {
            @Override
            public void onExecute(Activity activity) {

                PromptVisitor progressVisitor = new PromptVisitor(activity);
                progressVisitor.setLayout(R.layout.dialog_prompt_edit_top_part);
                progressVisitor.setGravity(Gravity.TOP);

                UIPOPDialog popDialog = new UIPOPDialog(activity, R.style.UIDialogTopInAnim,WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
                popDialog.setViewPartVisitor(progressVisitor);
                popDialog.setEnableDim(true);
                popDialog.show(activity);

                // 假设你已经有了一个布局文件popup_layout.xml，这是PopupWindow的内容
//                LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//                View popupView = inflater.inflate(R.layout.dialog_prompt_edit_top_part, null);
//                // 创建PopupWindow实例
//                final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
//
//                // 设置PopupWindow可以获取焦点，否则无法响应点击事件
//                popupWindow.setFocusable(true);
//
//                // 设置PopupWindow的外部点击事件，点击外部时PopupWindow消失
//                popupWindow.setOutsideTouchable(true);
//                popupWindow.setAnimationStyle(R.style.UIDialogTopInAnim);
//
//                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//                    @Override
//                    public void onDismiss() {
//                        setWindowBackgroundDim(activity, false);
//                    }
//                });
//
//                View left_view = popupView.findViewById(R.id.dialog_btn_left);
//                View right_view = popupView.findViewById(R.id.dialog_btn_right);
//                if (left_view != null) {
//                    left_view.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            popupWindow.dismiss();
//                        }
//                    });
//                }
//
//                if (right_view != null) {
//                    right_view.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            popupWindow.dismiss();
//                        }
//                    });
//                }
//
//                // 显示PopupWindow，设置PopupWindow顶部对齐
//                popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
//                setWindowBackgroundDim(activity, true);
            }
        }).build());
        //----------------------------------------------------------------
        return list;
    }

    // 设置背景的透明度
    private void setWindowBackgroundDim(Activity activity, boolean dim) {
        Window window = activity.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        if (dim) {
            layoutParams.alpha = 0.5f;  // 设置半透明（0.0f 表示完全透明，1.0f 表示完全不透明）
        } else {
            layoutParams.alpha = 1.0f;  // 恢复为不透明
        }
        window.setAttributes(layoutParams);
    }


    @Override
    public String getAppName() {
        return "devkit app";
    }
}
